home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Learn C++ (CodeWarrior) / Chap 09.02 - multInherit / multInherit.cp < prev    next >
Text File  |  1995-10-21  |  1KB  |  99 lines

  1. #include <iostream.h>
  2. #include <string.h>
  3.  
  4. const short kMaxStringLength = 40;
  5.  
  6.  
  7. //---------------------------------------  Predator
  8.  
  9. class Predator
  10. {
  11.     private:
  12.         char    favoritePrey[ kMaxStringLength ];
  13.         
  14.     public:
  15.                 Predator( char *prey );
  16.                 ~Predator();
  17. };
  18.  
  19. Predator::Predator( char *prey )
  20. {
  21.     strcpy( favoritePrey, prey );
  22.     
  23.     cout << "Favorite prey: "
  24.         << prey << "\n";
  25. }
  26.  
  27. Predator::~Predator()
  28. {
  29.     cout << "Predator destructor was called!\n\n";
  30. }
  31.  
  32.  
  33. //---------------------------------------  Pet
  34.  
  35. class Pet
  36. {
  37.     private:
  38.         char    favoriteToy[ kMaxStringLength ];
  39.         
  40.     public:
  41.                 Pet( char *toy );
  42.                 ~Pet();
  43. };
  44.  
  45. Pet::Pet( char *toy )
  46. {
  47.     strcpy( favoriteToy, toy );
  48.     
  49.     cout << "Favorite toy: "
  50.         << toy << "\n";
  51. }
  52.  
  53. Pet::~Pet()
  54. {
  55.     cout << "Pet destructor was called!\n";
  56. }
  57.  
  58.  
  59. //--------------------------  Cat:Predator,Pet
  60.  
  61. class Cat : public Predator, public Pet
  62. {
  63.     private:
  64.         short            catID;
  65.         static short    lastCatID;
  66.         
  67.     public:
  68.                 Cat( char *prey, char *toy );
  69.                 ~Cat();
  70. };
  71.  
  72. Cat::Cat( char *prey, char *toy ) :
  73.     Predator( prey ), Pet( toy )
  74. {
  75.     catID = ++lastCatID;
  76.     
  77.     cout << "catID: " << catID
  78.         << "\n---------\n";
  79. }
  80.  
  81. Cat::~Cat()
  82. {
  83.     cout << "Cat destructor called: catID = "
  84.         << catID << "...\n";
  85. }
  86.  
  87. short Cat::lastCatID = 0;
  88.  
  89.  
  90. //---------------------------------------  main()
  91.  
  92. int    main()
  93. {
  94.     Cat    TC( "Mice", "Ball of yarn" );
  95.     Cat    Benny( "Crickets", "Bottle cap" );
  96.     Cat    Meow( "Moths", "Spool of thread" );
  97.     
  98.     return 0;
  99. }